package com.yejiali.backend.exception;

import com.yejiali.backend.common.apicode.ResponseCodeEnum;
import com.yejiali.backend.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author:yejiali
 * @Date:2025/6/16-06-16-13:45
 * @Decsription:全局异常处理器
 * @Version:1.0
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     * @param e 自定义异常
     * @return 包含错误信息的响应对象
     */
    @ExceptionHandler(BusinessException.class)
    public Response handleSystemException(BusinessException e) {
        log.error("业务异常：", e);
        return Response.error(e.getCode(), e.getMessage());
    }

    /**
     * 处理其他异常
     * @param e 其他异常
     * @return 包含错误信息的响应对象
     **/
    @ExceptionHandler(Exception.class)
    public Response handleException(Exception e) {
        log.error("全局异常处理器捕获异常：", e);
        return Response.error(ResponseCodeEnum.INTERNAL_SERVER_ERROR.getCode(), "系统内部错误");
    }
}